package com.guhappy.model.user;

// Generated Jul 19, 2011 6:12:32 PM by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.guhappy.util.HibernateListener;

/**
 * Home object for domain model class GlobalStocks.
 * @see  bd
 * @author  B.D
 */
public class GlobalStockHome {

	private static final Logger log = Logger.getLogger(GlobalStockHome.class);

	private static final SessionFactory sessionFactory = getSessionFactory();

	protected static SessionFactory getSessionFactory() {
		SessionFactory sessionFactory = 
	         (SessionFactory) ServletActionContext.getServletContext()
                    .getAttribute(HibernateListener.KEY_NAME);
		return sessionFactory;
	}

	public static void persist(GlobalStock transientInstance) {
		log.debug("persisting GlobalStocks instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public static void attachDirty(GlobalStock instance) {
		log.debug("attaching dirty GlobalStocks instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void attachClean(GlobalStock instance) {
		log.debug("attaching clean GlobalStocks instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void delete(GlobalStock persistentInstance) {
		log.debug("deleting GlobalStocks instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public static GlobalStock merge(GlobalStock detachedInstance) {
		log.debug("merging GlobalStocks instance");
		try {
			GlobalStock result = (GlobalStock) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public static GlobalStock findById(java.lang.String id) {
		log.debug("getting GlobalStocks instance with id: " + id);
		Session session=null;
		GlobalStock stock=null;
		try{
			session = sessionFactory.getCurrentSession();
			Query q = session.createQuery("from com.guhappy.model.user.GlobalStock c where c.id = :id");
			q.setString("id", id);
			stock=(GlobalStock) q.uniqueResult();
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return stock;
	}
	
	public static GlobalStock findByTicker(String ticker){
		GlobalStock stock = null;
		try{
		stock = (GlobalStock) sessionFactory.getCurrentSession().createCriteria(GlobalStock.class).add(Restrictions.eq("sticker", ticker.trim())).uniqueResult();
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return stock;
	}
	
	public static ContestStockRecord getStockRecord(String ticker){
		ContestStockRecord stockRecord = new ContestStockRecord();
		StringBuffer buf = new StringBuffer();
		StringBuffer buf2 = new StringBuffer();
		buf.append("select count (distinct gh_contest_id ), count(GH_CONTPLAYER_ID) from VW_CONTEST_STOCK where f_stockid = '");
		buf.append(ticker);
		buf.append("'");
		ScrollableResults rset = sessionFactory.getCurrentSession().createSQLQuery(buf.toString()).scroll();
		while (rset.next()){		
			stockRecord.setTotal_contest_attend(((BigDecimal)rset.get(0)).intValue());
			stockRecord.setTotal_pick(((BigDecimal)rset.get(1)).intValue());
		}

		buf2.append("select count (distinct gh_contest_id ), count(GH_CONTPLAYER_ID) from VW_CONTEST_STOCK where f_stockid = '");
		buf2.append(ticker);
		buf2.append("' and GH_CONTSTOCK_PRIZE >0.0");
		rset = sessionFactory.getCurrentSession().createSQLQuery(buf2.toString()).scroll();
		while (rset.next()){
			stockRecord.setTotal_win(((BigDecimal)rset.get(0)).intValue());
		}
		return stockRecord;
	}
}
